package com.yunkeji.admin.controller.pay;

import cn.dev33.satoken.annotation.SaCheckPermission;

import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.core.validate.AddGroup;
import com.yunkeji.common.core.validate.EditGroup;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.common.utils.poi.ExcelUtil;
import com.yunkeji.dao.mapper.AppUserMapper;
import com.yunkeji.dao.vo.AppUserVo;
import com.yunkeji.dao.vo.PayDetailVo;
import com.yunkeji.system.domain.bo.ManualRechargeBo;
import com.yunkeji.system.domain.bo.PayDetailBo;
import com.yunkeji.system.service.IPayDetailService;

import lombok.RequiredArgsConstructor;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 充值明细
 *
 * @author zhang
 * @date 2024-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/payDetail")
public class PayDetailController extends BaseController {

    private final IPayDetailService iPayDetailService;
    private final AppUserMapper appUserMapper;

    /** 查询充值明细列表 */
    @SaCheckPermission("system:payDetail:list")
    @GetMapping("/list")
    public TableDataInfo<PayDetailVo> list(PayDetailBo bo, PageQuery pageQuery) {
        return iPayDetailService.queryPageList(bo, pageQuery);
    }

    /** 导出充值明细列表 */
    @SaCheckPermission("system:payDetail:export")
    @Log(title = "充值明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayDetailBo bo, HttpServletResponse response, PageQuery pageQuery) {
        TableDataInfo<PayDetailVo> info = iPayDetailService.queryPageList(bo, pageQuery);
        List<PayDetailVo> rows = info.getRows();
        // 当前导出用户id
        List<Long> collect = rows.stream().map(PayDetailVo::getUserId).collect(Collectors.toList());
        List<AppUserVo> appUserVos = appUserMapper.selectVoBatchIds(collect);
        Map<Long, Long> collect1 =
                appUserVos.stream().collect(Collectors.toMap(AppUserVo::getId, AppUserVo::getPid));
        rows.forEach(
                item -> {
                    item.setIdParent(collect1.get(item.getUserId()));
                });

        // 父级ID
        List<Long> collectedPid =
                appUserVos.stream().map(AppUserVo::getPid).collect(Collectors.toList());
        List<AppUserVo> pUser = appUserMapper.selectVoBatchIds(collectedPid);
        Map<Long, Long> pPlatfromIdMap =
                pUser.stream()
                        .collect(Collectors.toMap(AppUserVo::getId, AppUserVo::getPlatformId));
        rows.forEach(
                appUserVo -> {
                    appUserVo.setPlatformIdParent(pPlatfromIdMap.get(appUserVo.getIdParent()));
                });

        ExcelUtil.exportExcel(rows, "充值明细", PayDetailVo.class, response);
    }

    /** 手动充值 */
    @SaCheckPermission("system:payDetail:add")
    @Log(title = "手动充值", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/manualRecharge")
    public R<Void> manualRecharge(@RequestBody ManualRechargeBo bo){
        iPayDetailService.manualRecharge(bo);
        return R.ok();
    }

    /**
     * 获取充值明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:payDetail:query")
    @GetMapping("/{id}")
    public R<PayDetailVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iPayDetailService.queryById(id));
    }

    /** 新增充值明细 */
    @SaCheckPermission("system:payDetail:add")
    @Log(title = "充值明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayDetailBo bo) {
        return toAjax(iPayDetailService.insertByBo(bo));
    }

    /** 修改充值明细 */
    @SaCheckPermission("system:payDetail:edit")
    @Log(title = "充值明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayDetailBo bo) {
        return toAjax(iPayDetailService.updateByBo(bo));
    }

    /**
     * 删除充值明细
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:payDetail:remove")
    @Log(title = "充值明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iPayDetailService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
